package com.jt.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class  JDKProxyFactory {
    public static Object getProxy(final Object targe){
        ClassLoader classLoader = targe.getClass().getClassLoader();
        Class<?>[] interfaces = targe.getClass().getInterfaces();
        Object proxy = Proxy.newProxyInstance(classLoader, interfaces, new InvocationHandler() {
            //代理对象执行目标方法时执行
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                //让用户执行目标方法
                long start = System.currentTimeMillis();
                Object result = method.invoke(targe, args);
                Thread.sleep(2000);
                long end = System.currentTimeMillis();
                System.out.println("程序执行："+(end-start)+"毫秒");

                //将返回值传递给调用者
                return result;
            }
        });
        return proxy;
    }
}
